import axios from 'axios'
import config from '@/config'
import { getToken } from '@/utils/auth'
import { formatStrTime } from '@/utils/index'

export function download (url,params){
    axios.get(process.env.BASE_API + url,{
        responseType: 'blob',// 表明返回服务器返回的数据类型,  
        headers:{
            'X-Code':config.app.name,
            'X-Token':getToken(),
        },      
        params: params
    }).then((res)=>{
        const content = res
        const blob = new Blob([content.data])
        const fileName = formatStrTime(new Date(),'yyyyMMddHHmmss') +'.xlsx'        
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
    });
}